﻿/*
(c) 2010 - Open Source Business Foundation e. V. (www.osbf.de)
This software is provided under Microsoft Reciprocal License (Ms-RL)
(http://www.microsoft.com/opensource/licenses.mspx#Ms-RL)
All other rights reserved
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MicroFormatProcessing
{
    /// <summary>
    /// Convenience methods for user
    /// </summary>

    public static class UserUtil
    {
        public const int OK = 0;
        public const int UNKNOWN_USER = 1;
        public const int WRONG_PASSWORD = 2;

        public static void CreateUser(string userId, string password)
        {
            UserRepository userRepository = new UserRepository();
            userRepository.Add(new User(userId, password));
        }

        public static int CheckUser(string userId, string password)
        {
            UserRepository userRepository = new UserRepository();
            foreach (User mfUser in userRepository.Select(userId))
            {
                if ((password != null) && password.Equals(mfUser.GetPassword()))
                {
                    return OK;
                }
                else
                {
                    return WRONG_PASSWORD;
                }
            }
            return UNKNOWN_USER;
        }

        public static Session CreateSession(string userId)
        {
            if (userId != null)
            {
                SessionRepository sessionRepository = new SessionRepository();
                string dayKey = DateTime.UtcNow.ToString("yyyyMMdd");
                foreach (Session oldSession in sessionRepository.SelectByDayAndUser(dayKey, userId))
                {
                    oldSession.LastRequest = DateTime.UtcNow;
                    sessionRepository.Update(oldSession);
                    return oldSession;
                }
                byte[] userBytes = System.Text.Encoding.UTF8.GetBytes(userId.Substring(0, 2));
                string sessionId = String.Format("{0:X2}{1:X2}{2:X16}", userBytes[0], userBytes[1], long.MaxValue - DateTime.UtcNow.Ticks);
                Session newSession = new Session(dayKey, sessionId);
                newSession.LastRequest = DateTime.UtcNow;
                newSession.UserId = userId;
                sessionRepository.Add(newSession);
                return newSession;
            }
            else
            {
                return null;
            }
        }

        public static Session GetSesssion(string sessionId)
        {
            if (sessionId != null)
            {
                SessionRepository sessionRepository = new SessionRepository();
                string dayKey = DateTime.UtcNow.ToString("yyyyMMdd");
                foreach (Session mfSession in sessionRepository.SelectByDayAndSessionId(dayKey, sessionId))
                {
                    return mfSession;
                }
            }
            return null;
        }
    }
}